package com.ardf.controller;

import com.ardf.common.CodeEnum;
import com.ardf.common.ReturnResult;
import com.ardf.entity.Project;
import com.ardf.entity.Result;
import com.ardf.entity.dto.ProjectStartDto;
import com.ardf.service.IProjectService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ShiYunHao
 * @since 2025-01-06
 */
@RestController
@RequestMapping("/ardf/project")
public class ProjectController {

    private final IProjectService projectService;

    public ProjectController(IProjectService projectService) {
        this.projectService = projectService;
    }

    /**
     * 批量添加
     */
    @PostMapping("/addBatch")
    public void addBatch(@RequestBody List<Project> projects) {
        projectService.saveAll(projects);
    }

    /**
     * 分页
     *
     * @param pageSize 每页多少条
     * @param pageNum  第几页
     * @return 分页数据
     */
    @GetMapping("/page")
    public ResponseEntity<Page<Project>> page(@RequestParam("pageSize") Integer pageSize,
                                              @RequestParam("pageNum") Integer pageNum,
                                              @RequestParam("competitionId") Long competitionId) {
        Page<Project> page = projectService.page(new Page<>(pageNum, pageSize), new LambdaQueryWrapper<Project>()
                .eq(Project::getCompetitionId, competitionId));
        return ResponseEntity.ok(page);
    }

    /**
     * 根据赛事id获取全部项目
     */
    @GetMapping("/get")
    public ResponseEntity<List<Project>> get(@RequestParam("competitionId") Long competitionId) {
        List<Project> list = projectService.list(new LambdaQueryWrapper<Project>()
                .eq(Project::getCompetitionId, competitionId));
        return ResponseEntity.ok(list);
    }

    /**
     * 按照项目类型查询项目
     */
    @GetMapping("/getByType")
    public ResponseEntity<List<Project>> getByType(@RequestParam("type") Byte type, @RequestParam("competitionId") Long competitionId) {
        List<Project> list = projectService.list(new LambdaQueryWrapper<Project>().eq(Project::getType, type)
                .eq(Project::getCompetitionId, competitionId));
        return ResponseEntity.ok(list);
    }

    /**
     * 批量删除
     */
    @PostMapping("/deleteBatch")
    public void deleteBatch(@RequestBody List<Long> ids) {
        projectService.deleteAll(ids);
    }

    /**
     * 单个删除
     */
    @PostMapping("/deleteById")
    public void deleteById(@RequestParam("id") Long id) {
        projectService.deleteOne(id);
    }

    /**
     * 编辑开始时间
     */
    @PostMapping("/update")
    public void edit(@RequestParam("id") Long id, @RequestParam("startTime") LocalDateTime startTime) {
        projectService.update(new LambdaUpdateWrapper<Project>()
                .set(Project::getStartTime, startTime)
                .eq(Project::getId, id));
    }

    /**
     * 获取最近的项目
     */
    @GetMapping("/getRecentProject")
    public ResponseEntity<ProjectStartDto> getRecentProject(@RequestParam("competitionId") Long competitionId) {
        return ResponseEntity.ok(projectService.getRecentProject(competitionId));
    }

    /**
     * 获取最早的项目
     */
    @GetMapping("/getEarliestProject")
    public ResponseEntity<ReturnResult> getEarliestProject(@RequestParam("competitionId") Long competitionId) {
        ReturnResult result=new ReturnResult(CodeEnum.SUCCESS);
        result.setData(projectService.getEarliestProject(competitionId));
        return ResponseEntity.ok(result);
    }

    /**
     * 检查比赛项目设置是否合理
     */
    @GetMapping("/checkReasonableProTime")
    public ResponseEntity<ReturnResult> checkReasonableProTime(@RequestParam("competitionId") Long competitionId) {
        ReturnResult result=new ReturnResult(CodeEnum.SUCCESS);
        result.setData(projectService.checkReasonableProTime(competitionId));
        return ResponseEntity.ok(result);
    }
}
